#include "cclientserverdiscardorder.h"
#include "globalStructs.h"
#include <QNetworkRequest>
#include <QUrl>
#include <QMessageBox>
CClientServerDiscardOrder::CClientServerDiscardOrder()
{
    m_commandID = CLIENT_SERVER_DISCARD_ORDER;
    m_ArgCount = 1; // for we have 1 factors not including the "command ID"
    m_networkAccessManager = new QNetworkAccessManager(this);
}

CClientServerDiscardOrder::~CClientServerDiscardOrder()
{
    delete m_networkAccessManager;

    if (m_pListReply != NULL)
        delete m_pListReply;
}

QList<QString>& CClientServerDiscardOrder::formInteresedItemList()
{
    m_interestedList.append(QString("Person ID"));

    return m_interestedList;
}

QMultiMap<QString, QString>* CClientServerDiscardOrder::formResult()
{
    m_xmlString = QString::fromUtf8(m_pListReply->readAll());
    if ( m_xmlString.isEmpty() )
    {
        // no contents have been read or an error occures
        return NULL;
    }

    if(this->isReplyStringRight() == false)
    {
        return NULL;
    }

    this->formInteresedItemList();

    QDomElement root = m_pXMLDocument->documentElement();

    QDomElement element = root.firstChildElement("discards");

    if (!element.isNull())
    {
        QDomElement child = element.firstChildElement("discard");

        while (!child.isNull())
        {
            m_personID =  child.attribute("personId");
            m_result.insert(m_interestedList[0], m_personID);

            //lastly insert the command ID
            m_result.insert("Command ID", QString::number(m_commandID) );

            child = child.nextSiblingElement("discard");
        }
    }

    return &m_result;
}
